Repository navigation
fix(validators): reuse compiled validators for schemas without $id - #3260
Devpaul-01 wants to merge 3 commits into
Conversation
AjvJsonSchemaValidator.getValidator() only looked up an existing compiled validator when the schema declared a string $id. Every other schema went straight to engine.compile(), and AJV keeps each compiled schema in its internal cache for the life of the engine. Tool schemas rarely carry an $id, and Client.listTools() builds validators for every outputSchema on each call, so a client that refreshes its tool list kept growing the engine's cache by one entry per tool per refresh. Cache the compiled validator by the schema's JSON text, so a schema that is re-fetched as a new but equal object reuses the validator instead of compiling (and retaining) another one.
Asserts that an equal schema arriving as a new object compiles once, that schemas with different content stay separate, and that schemas with an $id keep going through the engine's own lookup.
🦋 Changeset detectedLatest commit: 6043a4b The changes in this PR will be included in the next version bump. This PR includes changesets to release 6 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@modelcontextprotocol/client
@modelcontextprotocol/codemod
@modelcontextprotocol/core
@modelcontextprotocol/server
@modelcontextprotocol/server-legacy
@modelcontextprotocol/express
@modelcontextprotocol/fastify
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
|
Linking the earlier PRs that cache compiled validators, so they can be looked at together. #2626 (9 Aug) and #2950 (4 Oct) are still open with This PR changes only the no- |
Motivation and Context
AjvJsonSchemaValidator.getValidator()only looked up an existing compiled validator when the schema declared a string$id. Every other schema went straight toengine.compile(), and AJV keeps each compiled schema in its internal cache for the lifetime of the engine. The provider holds one long-lived engine, so compiling an equal schema again retains another copy each time. Schemas received over the wire are freshly parsed objects, so AJV's own identity-keyed cache never hits for them.This addresses the memory growth described in #2605 on
main. One correction to the issue's framing: on currentmain,Client.listTools()no longer callsgetValidator()for every tool on every call (the response cache sits in front of it). The same unbounded growth still happens on other paths that call it with a schema that has no$id:Server.elicitInputform validation compilesrequestedSchemaon every accepted response.Client.callTool(..., { toolDefinition })compiles the supplied definition in isolation on every call.list_changedcompiles everyoutputSchemaagain.Fixing it in the provider covers all of them with one small change.
What changed
getValidator()now reuses the compiled validator for a schema without$id, keyed by the schema's JSON text (_compileWithoutId). The$idpath is untouched.How Has This Been Tested?
Measured on
mainwith 50 distinct tool-like schemas passed as new objects over 40 simulated refreshes, forcing GC before each heap reading:_cacheentries after 40 refreshesajvCompileReuse.test.ts: the first test fails onmain(expected "compile" to be called 1 times, but got 5 times) and passes with the fix (verified by reverting onlyajvProvider.ts).core-internal: 1862 tests passed.client: 1116 passed.server: 725 passed.tsc --noEmitpass.Breaking Changes
None.
Types of changes
Additional context
main. The issue also reports the same pattern inv1.x; I can send that backport as a separate small PR.